<?php

class spAddToListComboAndTextBool extends spAddToListBase
{
	public function __construct( $data=null )
	{
		parent::__construct( $data );
		parent::setClass( "spAddToListBase spAddToListComboAndTextBool" );

/*		
		// this is where we insert already predefined items
		if ( isset($data["items"]) && count($data["items"]) )
		{
			$insert_here = $this->find("insert_here");
			
			foreach ( $data["items"] as $item )
			{
				$insert_here->addChild( $this->createCloneableContainer( array(
					"selected_value"=>$item["field"],
					"map"=>$item["map"],
					"formatted"=>$item["formatted"]
				)));
			}
		}		
		*/
	}
	
	protected function createInsertHtml( $param=array() )
	{
		$data = parent::getData();
		
		$selected_value = isset($param["selected_value"])?$param["selected_value"]:"";
		
		$formatted = isset($param["formatted"])?$param["formatted"]:"";
		
		$name 		= isset($data["name"])?$data["name"]:"";
		
		$input_class 	= isset($data["class"])?$data["class"]:"";
		$map 			= isset($param["map"])?$param["map"]:"";
		
		// FIXME: remove wordpress code and find a way to create the path to the image
		$img = get_option('home').'/wp-content/plugins/gym/sigmaphp/html/extended/dragbar_gray.png';
		$i = new spImage(array("src"=>$img));
		$i->addAttr( "align", "top" );
		
		$combobox = new spCombobox( array( 
			"name"			=>$data["name"]."_field[]", 
			"list"			=>$data["list"],
			"selected_value"=>$selected_value 
		));
		
		$comboboxFormat = new spCombobox( array( 
			"name" 	=> $data["name"]."_format[]",
			"list" => array(
				array("value"=>"on", "content"=>"on"),
				array("value"=>"off", "content"=>"off"),
			),
			"selected_value"=>$formatted
		));
		
		$table = new spTableExt;
		$table->addRowChilds( array(
			$i,
			$combobox,
			new spInputText( array( "class"=>$input_class, "name"=>$name."_map[]", "value"=>$map ) ),
			$comboboxFormat
		));

		return $table;		
	}	
	
	public function setElementDataString( $s )
	{
		parent::setElementDataString( $s );
		
		$insert_here = $this->find("insert_here");
		
		$obj = json_decode( $s, true );
		foreach ( $obj as $o )
		{
			$insert_here->addChild( $this->createCloneableContainer( array(
				"selected_value"=>$o["field"],
				"map"=>$o["map"],
				"formatted"=>$o["formatted"]
			)));
		}		
	}	
	
	public function updateFromRequest()
	{
		parent::updateFromRequest();

		// convience variables
		$data = parent::getData();
		$r = $_REQUEST;	

		$name = isset($data["name"])?$data["name"]:"";
		
		if ( isset($r[$name."_field"]) )		
		{
			$arField = array_slice( $r[$name."_field"], 1 );		// remove first element in array (the clonable object)
			$arMap = array_slice( $r[$name."_map"], 1 );			// remove first element in array (the clonable object)
			$arFormat = array_slice( $r[$name."_format"], 1 );	// remove first element in array (the clonable object)
			
			$field = current( $arField );
			$map = current( $arMap );
			$format = current( $arFormat );
			
			$obj = array();
			while ( $field!==FALSE )
			{
				$obj[] = array( "field"=>$field, "map"=>$map, "formatted"=>$format );
				$field = next( $arField );
				$map = next( $arMap );
				$format = next( $arFormat );
			}
			
			$this->setElementDataString( json_encode( $obj ) );
		}
	}
}

?>